﻿Imports Co9320.NamaAlert.Extension
Imports Co9320.NamaAlert.Model

Namespace ViewModel

    Public Class PluginViewModel
        Inherits ViewModelBase

        Private WithEvents Plugin As PluginWrapper

        Public Sub New()
        End Sub

        Public Sub New(ByVal plugin As PluginWrapper)
            Me.Plugin = plugin
            _HasEntity = True
        End Sub

        Private _HasEntity As Boolean
        Public ReadOnly Property HasEntity() As Boolean
            Get
                Return _HasEntity
            End Get
        End Property

        Public Sub SetEntity(ByVal plugin As PluginWrapper)
            _HasEntity = True
            Me.Plugin = plugin
        End Sub

#Region "wrapped properties"

        Public ReadOnly Property Name() As String
            Get
                Return If(HasEntity, Plugin.Name, Nothing)
            End Get
        End Property

        Public ReadOnly Property Description() As String
            Get
                Return If(HasEntity, Plugin.Description, Nothing)
            End Get
        End Property

        Public ReadOnly Property SmallIcon() As ImageSource
            Get
                Return If(HasEntity, Plugin.SmallIcon, Nothing)
            End Get
        End Property

        Public ReadOnly Property LargeIcon() As ImageSource
            Get
                Return If(HasEntity, Plugin.LargeIcon, Nothing)
            End Get
        End Property

        Public ReadOnly Property HasEditableOptions() As Boolean
            Get
                Return If(HasEntity, Plugin.HasEditableOptions, False)
            End Get
        End Property

        Public ReadOnly Property HasAlertFunction() As Boolean
            Get
                Return If(HasEntity, Plugin.HasAlertFunction, False)
            End Get
        End Property

        Public ReadOnly Property RequireLiveProgramNotify() As LiveProgramNotify
            Get
                Return If(HasEntity, Plugin.RequireLiveProgramNotify, LiveProgramNotify.None)
            End Get
        End Property

        Public ReadOnly Property HasProgramAction() As Boolean
            Get
                Return If(HasEntity, Plugin.HasProgramAction, False)
            End Get
        End Property


        Private _Guid As Guid
        Public ReadOnly Property Guid() As Guid
            Get
                Return If(HasEntity, Plugin.Guid, _Guid)
            End Get
        End Property

#End Region

        '' TODO 名前変更 DataGridでソート時エラーになるのでI/F実装
        'Public ReadOnly Property Self() As PluginViewModel
        '    Get
        '        Return Me
        '    End Get
        'End Property

        Private _IsEnabled As Boolean
        Public Property IsEnabled() As Boolean
            Get
                Return _IsEnabled
            End Get
            Set(ByVal value As Boolean)
                _IsEnabled = value
                OnPropertyChanged("IsEnabled")
            End Set
        End Property

        Private _Settings As New PluginSettings
        Public Property Settings() As PluginSettings
            Get
                Return _Settings
            End Get
            Set(ByVal value As PluginSettings)
                _Settings = value
                OnPropertyChanged("Settings")
            End Set
        End Property



#Region "wrapped methods"

        Private IsInitialized As Boolean

        Private Sub CreateInstance()
            If Not IsInitialized Then
                Plugin.CreateInstance()
                Plugin.Initialize(Settings)
                IsInitialized = True
            End If
        End Sub

        Public Sub ExecuteAlert(ByVal program As Model.LiveProgram)
            If HasEntity Then
                CreateInstance()
                Plugin.ExecuteAlert(program)
            End If
        End Sub

        Public Sub ExecuteAction(ByVal program As Model.LiveProgram)
            If HasEntity Then
                CreateInstance()
                Plugin.ExecuteAction(program)
            End If
        End Sub

        Public Sub ExecuteWhenLiveProgramStarted(ByVal liveProgram As Model.LiveProgram)
            If HasEntity Then
                CreateInstance()
                Plugin.ExecuteWhenLiveProgramStarted(liveProgram)
            End If
        End Sub

        Public Sub EditOptions(ByVal dialogOwner As Window)
            If HasEntity Then
                CreateInstance()
                Plugin.EditOptions(dialogOwner)
            End If
        End Sub

#End Region

#Region "For EditOptions"

        Public Event RequestEditPluginOptions As EventHandler(Of EventArgs)
        Protected Overridable Sub OnEditPluginOptions(ByVal e As EventArgs)
            RaiseEvent RequestEditPluginOptions(Me, e)
        End Sub

        Private _IconAndTitle As IconAndTitleViewModel
        Public ReadOnly Property IconAndTitle() As IconAndTitleViewModel
            Get
                If _IconAndTitle Is Nothing Then
                    _IconAndTitle = New IconAndTitleViewModel(Me.SmallIcon, Me.Name)
                End If
                Return _IconAndTitle
            End Get
        End Property

        Private _EditOptionsCommand As ICommand
        Public ReadOnly Property EditOptionsCommand() As ICommand
            Get
                If _EditOptionsCommand Is Nothing Then
                    _EditOptionsCommand = New RelayCommand(New Action(Of Object)(AddressOf Me.EditOptions), New Predicate(Of Object)(AddressOf CanEditOptions))
                End If
                Return _EditOptionsCommand
            End Get
        End Property

        Private Sub EditOptions(ByVal obj As Object)
            If HasEntity Then
                OnEditPluginOptions(New EventArgs)
            End If
        End Sub

        Private Function CanEditOptions(ByVal obj As Object) As Boolean
            Return HasEditableOptions
        End Function

#End Region

        Public Overrides Function Serialize(ByVal name As String) As System.Xml.Linq.XElement
            Return <<%= name %>>
                       <Guid><%= Guid %></Guid>
                       <IsEnabled><%= IsEnabled %></IsEnabled>
                       <%= Settings.Serialize("Settings") %>
                   </>
        End Function

        Public Overrides Sub Deserialize(ByVal rootElement As System.Xml.Linq.XElement)

            Dim isEnabled As Boolean
            If Boolean.TryParse(rootElement.<IsEnabled>.Value, isEnabled) Then
                Me.IsEnabled = isEnabled
            End If

            Try
                _Guid = New Guid(rootElement.<Guid>.Value)
            Catch ex As Exception
                ' Ignore
            End Try

            Try
                Settings.Deserialize(rootElement.<Settings>.SingleOrDefault)
            Catch ex As Exception

            End Try

        End Sub


        Public Event RequestShowBalloonTip As EventHandler(Of ShowBalloonTipEventArgs)
        Protected Overridable Sub OnRequestShowBalloonTip(ByVal e As ShowBalloonTipEventArgs)
            RaiseEvent RequestShowBalloonTip(Me, e)
        End Sub

        Private Sub Plugin_RequestShowBalloonTip(ByVal sender As Object, ByVal e As Extension.ShowBalloonTipEventArgs) Handles Plugin.RequestShowBalloonTip
            RaiseEvent RequestShowBalloonTip(sender, e)
        End Sub
    End Class

End Namespace
